#include "SysLogManager.h"

#include <DatabaseManager/DatabaseManager.h>

SysLogManager* SysLogManager::getInstance()
{
    static SysLogManager instance;
    return &instance;
}

SysLogManager::SysLogManager(QObject *parent) : QObject(parent)
{
}

void SysLogManager::saveToDatabase(const QDateTime &time, LogType logType, InfoType infoType, const QString &content)
{
    // 使用 DatabaseManager 单例来插入日志
    DatabaseManager& dbManager = DatabaseManager::instance();

    if (!dbManager.isConnected()) {
        qWarning() << "数据库未连接，无法保存日志:" << content;
        return;
    }

    // 准备日志数据
    QVariantMap logData;
    logData["log_time"] = time.toString("yyyy-MM-dd hh:mm:ss.zzz");
    logData["log_type"] = static_cast<int>(logType);
    logData["info_type"] = static_cast<int>(infoType);
    logData["content"] = content;

    // 插入到数据库
    bool success = dbManager.insertData("system_logs", logData);

    if (!success) {
        qWarning() << "保存日志到数据库失败:" << dbManager.lastError() << "内容:" << content;
    }
}

void SysLogManager::log(LogType logType, InfoType infoType, const QString& content)
{
    QDateTime currentTime = QDateTime::currentDateTime();

    // 直接通过实例发射信号
    emit getInstance()->logReceived(currentTime,
                                    static_cast<int>(logType),
                                    static_cast<int>(infoType),
                                    content);

    // 这里还可以添加保存到数据库的功能
    saveToDatabase(currentTime, logType, infoType, content);
}

void SysLogManager::logInfo(InfoType infoType, const QString& content)
{
    log(LogType::LOG_INFO, infoType, content);
}

void SysLogManager::logSuccess(InfoType infoType, const QString& content)
{
    log(LogType::LOG_SUCCESS, infoType, content);
}

void SysLogManager::logError(InfoType infoType, const QString& content)
{
    log(LogType::LOG_ERROR, infoType, content);
}

void SysLogManager::logWarning(InfoType infoType, const QString& content)
{
    log(LogType::LOG_WARNING, infoType, content);
}

void SysLogManager::logDebug(InfoType infoType, const QString& content)
{
    log(LogType::LOG_DEBUG, infoType, content);
}

void SysLogManager::logDeviceConnected(const QString& deviceInfo)
{
    logSuccess(InfoType::DEVICE, QString("✅ 设备连接成功: %1").arg(deviceInfo));
}

void SysLogManager::logDeviceError(const QString& errorInfo)
{
    logError(InfoType::DEVICE, QString("❌ 设备错误: %1").arg(errorInfo));
}

void SysLogManager::logPathPlanningComplete(int points, int commands)
{
    logSuccess(InfoType::PATH_PLANNING,
               QString("✅ 路径规划完成，生成 %1 个路径点，%2 条指令").arg(points).arg(commands));
}

void SysLogManager::logCommandExecuted(const QString& command, bool isSuccess)
{
    if (isSuccess) {
        logSuccess(InfoType::COMMAND_EXEC, QString("✅ 命令执行成功: %1").arg(command));
    } else {
        logError(InfoType::COMMAND_EXEC, QString("❌ 命令执行失败: %1").arg(command));
    }
}

void SysLogManager::logDatabaseConnected(const QString& dbInfo)
{
    logSuccess(InfoType::DATABASE, QString("✅ 数据库连接成功: %1").arg(dbInfo));
}

void SysLogManager::logCameraError(const QString& errorInfo)
{
    logError(InfoType::CAMERA, QString("❌ 相机错误: %1").arg(errorInfo));
}

void SysLogManager::logImageLoaded(const QString& imageInfo)
{
    logSuccess(InfoType::IMAGE_PROCESSING, QString("✅ 图像加载成功: %1").arg(imageInfo));
}

void SysLogManager::logAutoExecutionProgress(int current, int total, const QString& step)
{
    int progress = (total > 0) ? (current * 100 / total) : 0;
    logInfo(InfoType::COMMAND_EXEC,
            QString("全自动进度: %1/%2 (%3%) - %4").arg(current).arg(total).arg(progress).arg(step));
}

QString SysLogManager::logTypeToString(LogType type)
{
    switch(type) {
    case LogType::LOG_DEBUG: return "debug";
    case LogType::LOG_INFO: return "info";
    case LogType::LOG_SUCCESS: return "success";
    case LogType::LOG_WARNING: return "warning";
    case LogType::LOG_ERROR: return "error";
    default: return "unknown";
    }
}

QString SysLogManager::infoTypeToString(InfoType type)
{
    switch(type) {
    case InfoType::SYSTEM: return "系统";
    case InfoType::DATABASE: return "数据库";
    case InfoType::DEVICE: return "设备";
    case InfoType::CAMERA: return "相机";
    case InfoType::IMAGE_PROCESSING: return "图像处理";
    case InfoType::PATH_PLANNING: return "路径规划";
    case InfoType::COMMAND_EXEC: return "指令执行";
    case InfoType::FIXTURE_CTRL: return "夹具控制";
    default: return "未知";
    }
}
